<template>
  <div id="make_knowledgeGraph">
    <div v-if="roles === 'teacher'" class="one-btns">
      <div class="title">知识图谱结构图</div>
      <!-- <div class="all-btn" @click="saveKnowledgeGraph()">保存知识图谱</div> -->
    </div>
    <!-- 树形图 -->
    <div class="content">
      <div class="graph-tree">
        <el-tree
          :data="treeData"
          node-key="value"
          default-expand-all
          :props="defaultProps"
          :expand-on-click-node="false"
        >
          <template #default="{ node, data }">
            <div class="custom-tree-node">
              <span @click="nodeClick(node, data)">{{ node.label }}</span>
              <!-- <div>
                <el-button
                  style="color: #457aff"
                  type="primary"
                  link
                  @click.stop="append(data)"
                >
                  <el-icon :size="12" color="#457aff" style="margin-right: 4px"
                    ><CirclePlus
                  /></el-icon>
                  新增
                </el-button>
                <el-button
                  style="color: #457aff"
                  type="primary"
                  link
                  @click.stop="remove(node, data)"
                >
                  <el-icon :size="12" color="#457aff" style="margin-right: 4px"
                    ><Close
                  /></el-icon>
                  删除
                </el-button>
              </div> -->
            </div>
          </template>
        </el-tree>
      </div>
      <div class="graph-form">
        <div v-if="nodeValue" class="graph-form-content">
          <div class="node-name">
            <span>名称</span>
            <div class="name-content">{{ nodeName }}</div>
          </div>
          <div class="node-knowledge">
            <span>知识点</span>
            <div class="knowledge-content">{{ nodeKnowledge }}</div>
          </div>
        </div>
        <!-- <el-button class="saveBtn" @click="saveAddNode()">保存</el-button> -->
        <div class="no-data" v-else>
          <img src="@/assets/img/no-data.png" alt="" />
          <span>暂无内容</span>
        </div>
      </div>
    </div>
  </div>
</template>
    
<script>
import HomeChart2 from "@/components/HomeChart2.vue";
export default {
  components: {
    HomeChart2,
  },
  name: "ZhishiIndex",
  data() {
    return {
      courseId: "",
      isAdd: false,
      roles: "",
      nodeName: "",
      nodeKnowledge: "",
      nodeValue: "",
      defaultProps: {
        label: "label",
        children: "children",
      },
      treeData: [],
    };
  },
  created() {
    this.roles = JSON.parse(localStorage.getItem("userInfo")).roles[0];
    this.courseId = JSON.parse(localStorage.getItem("courseId"));
    this.getList();
  },
  methods: {
    getList() {
      const url = `/tes/graph/graph-relation-course-info?courseId=${this.courseId}`;
      // const params = {
      //   bookInfoId: 1, // 使用 params 传递查询参数
      // };

      this.$axios.get(url).then((res) => {
        this.treeData = res.data;
      });
    },

    // 点击节点
    nodeClick(node, data) {
      // 检查data中是否有value属性
      if (data && data.value) {
        this.nodeValue = data.value;
        this.$axios
          .get(
            `/tes/graph/graph-relation-course-info/content?id=${this.nodeValue}`
          )
          .then((res) => {
            this.nodeName = data.label;
            this.nodeKnowledge = res.data;
          });
      } else {
        this.nodeValue = "";
      }
    },

    // 新增节点
    append(data) {
      this.isAdd = true;
    },

    // 删除节点
    remove(node, data) {},

    // 保存知识图谱
    saveKnowledgeGraph() {},
  },
};
</script>
  <style lang="scss" scoped>
#make_knowledgeGraph {
  width: 100%;
  background-color: #f3f5fa;
  padding: 20px 30px 15px;
  box-sizing: border-box;
  overflow-x: hidden;
  .one-btns {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    .title {
      font-size: 18px;
      color: #596184;
    }
  }
  .content {
    height: 100%;
    display: flex;
    justify-content: space-between;
    box-sizing: border-box;
    .graph-tree {
      padding: 30px;
      overflow-y: auto;
      width: calc(50% - 10px);
      height: 580px;
      background: #ffffff;
      border-radius: 10px;
      box-sizing: border-box;
      &::-webkit-scrollbar {
        width: 5px;
      }
      &::-webkit-scrollbar-thumb {
        background-color: #ffffff;
        border-radius: 5px;
      }
      &::-webkit-scrollbar-track {
        background-color: #ffffff;
      }
    }

    .nograph-form {
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 50px 40px;
      width: calc(50% - 10px);
      height: 580px;
      background: #ffffff;
      border-radius: 10px;
      box-sizing: border-box;
    }

    .graph-form {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      padding: 50px 40px;
      width: calc(50% - 10px);
      height: 580px;
      background: #ffffff;
      border-radius: 10px;
      box-sizing: border-box;
      .graph-form-content {
        width: 100%;
      }
      .no-data {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        gap: 10px;
        width: 100%;
        height: 100%;
        font-size: 14px;
        color: #596184;
      }
      .node-name {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        width: 100%;
        margin-bottom: 20px;
        span {
          flex-shrink: 0;
          font-size: 14px;
          color: #596184;
          margin-bottom: 13px;
        }
        .name-content {
          padding: 10px;
          width: 100%;
          height: 40px;
          border: 1px solid #ccc;
          border-radius: 4px;
          font-size: 14px;
          color: #596184;
          box-sizing: border-box;
        }
      }
      .node-knowledge {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        width: 100%;
        margin-bottom: 20px;
        // :deep(.el-textarea__inner) {
        //   height: 323px;
        // }
        span {
          flex-shrink: 0;
          font-size: 14px;
          color: #596184;
          margin-bottom: 13px;
        }
        .knowledge-content {
          padding: 10px;
          width: 100%;
          height: 323px;
          word-wrap: break-word;
          border: 1px solid #ccc;
          border-radius: 4px;
          font-size: 14px;
          color: #596184;
          box-sizing: border-box;
          overflow-y: auto;
          &::-webkit-scrollbar {
            width: 5px;
          }
          &::-webkit-scrollbar-thumb {
            background-color: #c1c1c1;
            border-radius: 5px;
          }
          &::-webkit-scrollbar-track {
            background-color: #ffffff;
          }
        }
      }
      .saveBtn {
        background-color: #155eef;
        color: #fff;
      }
    }
    .custom-tree-node {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 14px;
      padding-right: 8px;

      // 默认隐藏操作按钮
      > div {
        opacity: 0;
        transition: opacity 0.2s ease-in-out;
      }

      // 鼠标悬停时显示操作按钮
      &:hover > div {
        opacity: 1;
      }
    }
  }
  .all-btn {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    gap: 4px;
    height: 40px;
    line-height: 40px;
    background: #457aff;
    border-radius: 4px;
    font-size: 14px;
    color: #fff;
    padding: 0 18px;
    cursor: pointer;
  }
}
</style>